#!/usr/bin/env python

from googlevoice import Voice
import re
import sys

voice = Voice()
voice.login()

conversations = []
page = 0
while True:
    page += 1
    voice.voicemail(terms={ 'page':('p%d' % page) })
    
    jsonData = voice.voicemail.data
    
    if 'messages' not in jsonData or len(jsonData['messages']) < 1:
        break
    for message in jsonData['messages']:
        # now retrieve the message details for each collected ID
        # alternatively, collect the IDs from every page and then fetch the details
        getattr(voice,'message')(terms={ 'messageId':jsonData['messages'][message]['id'] })
        conversations.append( getattr(voice,'message').data['messages'] )
    print 'page %d' % page

## As a proof of concept, just print out the data structures for the
## earliest and latest conversation. You can iterate over the
## 'conversations' list to get the data for all of the conversations.

print len(conversations)
print '\n%s\n\n%s' % (repr(conversations[0]), repr(conversations[-1]))

import json
print json.dumps(conversations[0],sort_keys=True, indent=4)
